package com.gomefinance.excelutils;

import java.io.FileOutputStream;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;


public class IRowWriteImpl<T extends Object> implements IRowWrite {

	// 存表数据
	private List<T> list;

	// 表头集合
	private Map<String, String> titleMap;

	public IRowWriteImpl(List<T> list, Map<String, String> titleMap) {
		this.list = list;
		this.titleMap = titleMap;
	}

	public Map<String, String> getTitleMap() {
		return titleMap;
	}

	public void setTitleMap(Map<String, String> titleMap) {
		this.titleMap = titleMap;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	/**
	 * 业务逻辑实现方法
	 * 

	 * @throws Exception
	 */
	@Override
	public void setRows(String filePath) throws Exception {
		FileOutputStream in = null;
		try {
			in = new FileOutputStream(filePath);
			// excel数据添加处理
			HSSFWorkbook wBook = expExcel();
			// 创建文件
			wBook.write(in);
		} catch (Exception e) {
			throw e;
		} finally {
			if (null != in && in.getFD().valid()) {
				in.close();
			}
		}

	}

	/**
	 * 创建工作簿
	 * 
	 * @return
	 * @throws Exception
	 */
	public HSSFWorkbook expExcel() throws Exception {
		Date date = new Date();
		// 创建03工作簿
		HSSFWorkbook wBook = new HSSFWorkbook();
		// 创建工作表
		HSSFSheet sheet = wBook.createSheet("sheet0");
		// 标题行初始化
		HSSFRow titleRow = sheet.createRow((short) 0);
		// 设置标题行高度
		titleRow.setHeight((short) 500);
		// 设置标题样式
		HSSFCellStyle titelStyle = getTitleStyle(wBook);
		// 创建单元格
		HSSFCell titelCell = titleRow.createCell(0);
		// 添加样式
		titelCell.setCellStyle(titelStyle);
		// 设置内容
		//titelCell.setCellValue(PayConstant.EXPORT_TITLE_NAME);
		// 合并标题行
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, titleMap.size() - 1));
		// 表头初始化
		HSSFRow row2 = sheet.createRow((short) 1);
		row2.setHeight((short) 350);
		HSSFCellStyle tdStyle = getHeadStyle(wBook);
		Set<Entry<String, String>> headEntity = titleMap.entrySet();
		int x = 0;
		for (Entry<String, String> entry : headEntity) {
			HSSFCell tdCell = row2.createCell(x);
			tdCell.setCellStyle(tdStyle);
			tdCell.setCellValue(entry.getValue());
			x++;
		}
		// 行序号起始为删除的标题所在行的+当前的行数
		int rowNo = 2;
		// 填写数据
		for (int i = 0; i < list.size(); i++) {
			// 讲Map转为List
			T objMap = list.get(i);
			// 将对象转换为MAP
			Map<String, String> objectMap = toMap(objMap, titleMap, date, i + 1);
			// 获取行
			HSSFRow row = sheet.getRow(rowNo);
			// 如果当前行为空则新建行
			if (row == null) {
				row = sheet.createRow(rowNo);
			}

			int columnNum = 0;
			String value = "";
			for (Entry<String, String> entry : headEntity) {
				HSSFCell cell = row.createCell(columnNum);
				value = (String) objectMap.get(entry.getKey());
				if (value == null) {
					value = "";
				}
				// 写入单元格内容
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(value);
				columnNum++;
			}
			rowNo++;
		}
		// Excel宽度自适应
		int z = 0;
		for (@SuppressWarnings("unused")
		Entry<String, String> entry : headEntity) {
			// 列的宽度自适应
			sheet.autoSizeColumn((short) z);
			z++;
		}
		return wBook;

	}

	/**
	 * 设置表体的标题样式
	 * 
	 * @return
	 */
	public static HSSFCellStyle getTitleStyle(HSSFWorkbook workBook) {
		// 创建单元格样式
		HSSFCellStyle cellStyle = workBook.createCellStyle();
		// 设置单元格居中对齐
		cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
		// 设置单元格垂直居中对齐
		cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
		// 创建单元格内容显示不下时自动换行
		cellStyle.setWrapText(true);
		// 设置单元格字体样式
		HSSFFont font = workBook.createFont();
		// 设置字体加粗
		font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName("宋体");
		font.setFontHeight((short) 300);
		cellStyle.setFont(font);
		// 设置单元格边框为细线条
		cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
		return cellStyle;
	}

	/**
	 * 设置表头的单元格样式
	 * 
	 * @return
	 */
	public static HSSFCellStyle getHeadStyle(HSSFWorkbook workBook) {
		// 创建单元格样式
		HSSFCellStyle cellStyle = workBook.createCellStyle();
		// 设置单元格的背景颜色为淡蓝色
		cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
		cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
		// 设置单元格居中对齐
		cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
		// 设置单元格垂直居中对齐
		cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
		// 创建单元格内容显示不下时自动换行
		cellStyle.setWrapText(true);
		// 设置单元格字体样式
		HSSFFont font = workBook.createFont();
		// 设置字体加粗
		font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName("宋体");
		font.setFontHeight((short) 200);
		cellStyle.setFont(font);
		// 设置单元格边框为细线条
		cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
		return cellStyle;
	}

	/**
	 * 把对象转换为Map
	 * 
	 * @param object
	 * @param title
	 * @return
	 */
	public Map<String, String> toMap(T object, Map<String, String> titleMap, Date date, int index) throws Exception {
		Map<String, String> map = new LinkedHashMap<String, String>();
		return map;
	}
}